/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ABMArticulos.java
 *
 * Created on 18/07/2011, 20:27:23
 */

package ventanas;

import conexiones.AdministradorDeConexiones;
import dao.ArticulosDao;
import dao.RubrosDao;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import modelo.Articulo;
import modelo.Rubro;

/**
 *
 * @author Cristian Perez
 */
public class ABMArticulos extends javax.swing.JDialog {
    private DefaultTableModel modeloArticulos = new DefaultTableModel();
    private DefaultComboBoxModel modeloRubros = new DefaultComboBoxModel();
    private ArrayList listaAuxiliar;

    /** Creates new form ABMArticulos */
    public ABMArticulos(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        modeloArticulos = (DefaultTableModel) jTable1.getModel();
        jTable1.setModel(modeloArticulos);
        jComboBox2.setModel(modeloRubros);
        modeloRubros = (DefaultComboBoxModel) jComboBox2.getModel();

        estructurarTabla();
        listarTodosLosArticulos();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        jComboBox2 = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        btnAlta = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        btnListaComp = new javax.swing.JButton();
        btnArtStockMin = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Articulos");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jTable1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jComboBox1.setFont(new java.awt.Font("Tahoma", 3, 11));
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione...", "Codigo", "Descripcion", "Rubro" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel1.setText("Buscar por: ");

        jTextField1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jTextField1.setEnabled(false);
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextField1FocusLost(evt);
            }
        });
        jTextField1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jTextField1PropertyChange(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        btnBuscar.setFont(new java.awt.Font("Tahoma", 1, 12));
        btnBuscar.setText("Buscar");
        btnBuscar.setEnabled(false);
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        jComboBox2.setFont(new java.awt.Font("Tahoma", 3, 11));
        jComboBox2.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                .addGap(138, 138, 138))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnAlta.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnAlta.setText("Nuevo Articulo");
        btnAlta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaActionPerformed(evt);
            }
        });

        btnEliminar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnModificar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnSalir.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        btnListaComp.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnListaComp.setText("Ver Lista Completa");
        btnListaComp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnListaCompActionPerformed(evt);
            }
        });

        btnArtStockMin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnArtStockMin.setText("Articulos x debajo del S. minimo");
        btnArtStockMin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnArtStockMinActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnAlta, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnListaComp)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnArtStockMin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAlta, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnListaComp, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnArtStockMin, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 937, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-973)/2, (screenSize.height-521)/2, 973, 521);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnAltaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaActionPerformed
        // TODO add your handling code here:
        AltaArticulos ventaAltaArt = new AltaArticulos(this,true);
        ventaAltaArt.setVisible(true);
    }//GEN-LAST:event_btnAltaActionPerformed

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
        // TODO add your handling code here:
        listarTodosLosArticulos();
    }//GEN-LAST:event_formWindowGainedFocus

    private void btnListaCompActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnListaCompActionPerformed
        // TODO add your handling code here:
        listarTodosLosArticulos();
    }//GEN-LAST:event_btnListaCompActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        // TODO add your handling code here:
        Connection conn = null;
        if (jTable1.getRowCount() == 0) {
            JOptionPane.showMessageDialog(this, "No existen articulos a eliminar!!!", "Error", JOptionPane.INFORMATION_MESSAGE);
        }
        else if (jTable1.getSelectedRowCount() == 0){
            JOptionPane.showMessageDialog(this, "No ha seleccionado ningun articulo para eliminar...", "Error", JOptionPane.INFORMATION_MESSAGE);
        }
        else if (jTable1.getSelectedRowCount() < 2 ) {
            int rta = JOptionPane.showConfirmDialog(this,
                                        "Esta seguro de eliminar el articulo ?",
                                        "Eliminar",
                                        JOptionPane.YES_NO_OPTION);

            // Si no desea borrarlo, entonces no lo borra
            if(rta == JOptionPane.NO_OPTION) {
                return;
            }
            try {
                conn = AdministradorDeConexiones.obtenerConexion();
                /*Le pedimos a la listaAuxiliar el articulo que vamos a borrar*/
                Articulo articuloAborrar = (Articulo) listaAuxiliar.get(jTable1.getSelectedRow());
                //Eliminamos el item de la mesa, se borra de la base de datos
                ArticulosDao.getInstance().eliminar(conn, articuloAborrar);
                listarTodosLosArticulos();
            }
            catch(Exception e) {e.printStackTrace();}

        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        // TODO add your handling code here:
        Connection conn = null;
        if (jTable1.getRowCount() == 0) {
            JOptionPane.showMessageDialog(this, "No existen articulos para modificar...", "Error", JOptionPane.INFORMATION_MESSAGE);
        }
        else if (jTable1.getSelectedRowCount() == 0){
            JOptionPane.showMessageDialog(this, "No ha seleccionado ningun articulo para modificar...", "Error", JOptionPane.INFORMATION_MESSAGE);
        }
        else if (jTable1.getSelectedRowCount() == 1 ) {
            ModificarArticulo ventaModArt = new ModificarArticulo(this, true);
            ventaModArt.mostrarUsuario((Articulo) listaAuxiliar.get(jTable1.getSelectedRow()));
            ventaModArt.setVisible(true);
        }
        else if (jTable1.getSelectedRowCount() > 1) {
            JOptionPane.showMessageDialog(this, "No puede modificar mas de un articulo a la vez...", "Error", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btnModificarActionPerformed

    private void jTextField1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jTextField1PropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1PropertyChange

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        // TODO add your handling code here:
        if (jComboBox1.getSelectedIndex() == 1) {
            if (evt.getKeyCode() == 10) {
                filtrarCodigo();
                jTextField1.setText("");
            }
        }
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
        switch (jComboBox1.getSelectedIndex())
        {
            case 0: jTextField1.setEnabled(false);
                    jComboBox2.setEnabled(false);
                    btnBuscar.setEnabled(false);
                    modeloRubros.removeAllElements();
                break;
            case 1: jTextField1.setEnabled(true);
                    jTextField1.requestFocus();
                    jTextField1.setText("");
                    jComboBox2.setEnabled(false);
                    btnBuscar.setEnabled(false);
                    modeloRubros.removeAllElements();
                break;
            case 2: jTextField1.setEnabled(true);
                    jTextField1.requestFocus();
                    jTextField1.setText("");
                    jComboBox2.setEnabled(false);
                    btnBuscar.setEnabled(false);
                    modeloRubros.removeAllElements();
                break;
            case 3: jTextField1.setEnabled(false);
                    jComboBox2.setEnabled(true);
                    btnBuscar.setEnabled(true);
                    CargarComboRubros();
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
        listarArticulosXrubro();
        jComboBox1.setSelectedIndex(0);
        jComboBox2.setEnabled(false);
        btnBuscar.setEnabled(false);
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jTextField1KeyTyped

    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
        // TODO add your handling code here:
        if (evt.getKeyCode() != 27) {
            if (jComboBox1.getSelectedIndex() == 2)
                filtrarDescripcion();
        }
        else {
            jTextField1.setText("");
            jTextField1.setEnabled(false);
            jComboBox1.requestFocus();
        }
    }//GEN-LAST:event_jTextField1KeyReleased

    private void jTextField1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField1FocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1FocusGained

    private void jTextField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField1FocusLost
        // TODO add your handling code here:
        jTextField1.setText("");
    }//GEN-LAST:event_jTextField1FocusLost

    private void btnArtStockMinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnArtStockMinActionPerformed
        // TODO add your handling code here:
        ListadoDebajoSminimo ventaListadoSM = new ListadoDebajoSminimo(this, true);
        ventaListadoSM.setVisible(true);
    }//GEN-LAST:event_btnArtStockMinActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlta;
    private javax.swing.JButton btnArtStockMin;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnListaComp;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

    private void estructurarTabla() {
         //Estructuramos el modeloArticulos para la visualizacion en el jtable
        modeloArticulos.addColumn("DESCRIPCION");
        modeloArticulos.addColumn("P.COSTO");
        modeloArticulos.addColumn("P.VENTA");
        modeloArticulos.addColumn("STK ACTUAL");
        modeloArticulos.addColumn("STK MINIMO");
        modeloArticulos.addColumn("RUBRO");

        jTable1.getColumnModel().getColumn(0).setPreferredWidth(350);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(10);
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(10);
        jTable1.getColumnModel().getColumn(3).setPreferredWidth(10);
        jTable1.getColumnModel().getColumn(4).setPreferredWidth(10);
        jTable1.getColumnModel().getColumn(5).setPreferredWidth(90);
    }

     public void listarTodosLosArticulos() {
        Connection conn = null;
        listaAuxiliar = null;
        try {
            conn = AdministradorDeConexiones.obtenerConexion();
            //Recuperamos todos los articulos de la tabla de Articulos
            listaAuxiliar = ArticulosDao.getInstance().getTodos(conn);
            this.vaciarModeloArticulos();
            this.cargarModeloArticulos();
        }catch(Exception e) {e.printStackTrace();}
    }

     private void vaciarModeloArticulos() {
        modeloArticulos.setRowCount(0);
    }

     private void cargarModeloArticulos() {
        Iterator iteraArt = listaAuxiliar.iterator();
            while (iteraArt.hasNext()) {
                String[] arregloArt = new String[6];
                //Obtenemos cada articulo de la lista
                Articulo art = (Articulo) iteraArt.next();
                arregloArt[0] = art.getDescrip();
                arregloArt[1] = String.valueOf(art.getPreCosto());
                arregloArt[2] = String.valueOf(art.getPreVenta());
                arregloArt[3] = String.valueOf(art.getStkAct());
                arregloArt[4] = String.valueOf(art.getStkMin());
                arregloArt[5] = art.getRubro().getNombre();
                //Agregamos el arreglo al modelo
                modeloArticulos.addRow(arregloArt);
            }
    }

      private void CargarComboRubros() {
        Connection conn = null;
        ArrayList lista = null;
        try {
            conn = AdministradorDeConexiones.obtenerConexion();
            lista = RubrosDao.getInstance().getTodos(conn);
            Rubro rub = null;
            for (int i=0; i < lista.size(); i++) {
                rub = (Rubro)lista.get(i);
                modeloRubros.addElement(rub);
            }
        }catch(Exception e) {e.printStackTrace();}
    }

      private boolean Validarbusqueda(){
          if (jComboBox1.getSelectedIndex() == 0) {
              JOptionPane.showMessageDialog(this, "Debe seleccionar un criterio de busqueda...", "Error", JOptionPane.INFORMATION_MESSAGE);
              jComboBox1.requestFocus();
              return false;
          }
          else if (((jComboBox1.getSelectedIndex() == 1) | (jComboBox1.getSelectedIndex() == 2)) & (jTextField1.getText().trim().equals(""))) {
              JOptionPane.showMessageDialog(this, "Debe ingresar un valor para la busqueda...", "Error", JOptionPane.INFORMATION_MESSAGE);
              jTextField1.setText("");
              jTextField1.requestFocus();
              return false;              
          }
          return true;
      }

      private void filtrarDescripcion() {
          Connection conn = null;
          try {
              conn = AdministradorDeConexiones.obtenerConexion();
              String aBuscar = jTextField1.getText().toUpperCase().trim();
              if (ArticulosDao.getInstance().existeFiltroArt(conn, aBuscar)) {
                   listaAuxiliar = null;
                   listaAuxiliar = ArticulosDao.getInstance().getArticuloXstring(conn, aBuscar);
                   this.vaciarModeloArticulos();
                   this.cargarModeloArticulos();
              }
              else {
                   JOptionPane.showMessageDialog(this, "La descripcion no coincide con ningun articulo...", "Error", JOptionPane.INFORMATION_MESSAGE);
                   jTextField1.setText("");
                   return;
              }
          }catch(Exception e){e.printStackTrace();}
      }

      private void filtrarCodigo() {
          Connection conn = null;
          long cod = parsearNumero();
          ArrayList lista = new ArrayList();
          try {
              conn = AdministradorDeConexiones.obtenerConexion();
              if (ArticulosDao.getInstance().verificarExistenciaArt(conn, cod)) {
                   Articulo art = ArticulosDao.getInstance().getArticuloXcodigo(conn, cod);
                   listaAuxiliar = null;
                   lista.add(art);
                   listaAuxiliar = lista;
                   this.vaciarModeloArticulos();
                   this.cargarModeloArticulos();
              }
              else{
                    JOptionPane.showMessageDialog(this, "El codigo es inexistente...", "Error", JOptionPane.INFORMATION_MESSAGE);
                    jTextField1.setText("");
                    jTextField1.requestFocus();
                  }
          }catch(Exception e){
              e.printStackTrace();
          }
    }

      private long parsearNumero(){
          long codigo = 0;
          try {
              String codBarra = jTextField1.getText().trim();
              if (codBarra.charAt(0) == '2') {
                    String codAcargar = codBarra.substring(0, 6);
                    codigo = Long.parseLong(codAcargar);
                }
                else codigo = Long.parseLong(jTextField1.getText());
          }catch(Exception NumberFormatException) {
                JOptionPane.showMessageDialog(this, "El codigo debe ser un numero...", "Error", JOptionPane.INFORMATION_MESSAGE);
                jTextField1.setText("");
                jTextField1.requestFocus();
          }
          return codigo;
      }

      private void listarArticulosXrubro() {
        Connection conn = null;
        listaAuxiliar = null;
        try {
            conn = AdministradorDeConexiones.obtenerConexion();
            //Verificamos la existencia de articulos pertenecientes a la familia seleccionada
            if (ArticulosDao.getInstance().existeRubroEnArticulo(conn, (Rubro) jComboBox2.getSelectedItem())) {
                //Recuperamos los articulos de la tabla de Articulos filtrados por un numero de familia
                listaAuxiliar = ArticulosDao.getInstance().devolverArticulosXrubro(conn,(Rubro) jComboBox2.getSelectedItem() );
                this.vaciarModeloArticulos();
                this.cargarModeloArticulos();
            }
            else{
                JOptionPane.showMessageDialog(this, "No existe articulos con el rubro seleccionado...", "Error", JOptionPane.INFORMATION_MESSAGE);
            }
        }catch(Exception e) {e.printStackTrace();}
    }
}
